<template>
  <div class="app-container">
    <el-form :model="query" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="工序ID" prop="processId">
        <el-input
          v-model="queryParams.processId"
          placeholder="请输入工序ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="工位ID" prop="stationId">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入工位ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="设备ID" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="设备名称" prop="deviceId">
        <el-input
          v-model="query.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zndd:processdevice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zndd:processdevice:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zndd:processdevice:remove']"
        >批量解绑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zndd:processdevice:export']"
        >导出</el-button> -->
      <!-- </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processdeviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="部门ID" align="center" prop="deptId" /> -->
      <!-- <el-table-column label="工序" align="center" prop="processId" :formatter="processFomatter"/>
      <el-table-column label="工位" align="center" prop="stationId"  :formatter="stationFomatter"/> -->
      <!-- <el-table-column label="设备ID" align="center" prop="deviceId" /> -->
      <el-table-column label="设备名称" align="center" prop="device" />
      <el-table-column label="设备编号" align="center" prop="deviceNo" />
      <el-table-column label="设备状态" align="center" :formatter="deviceStateeFomatter"/>
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.processId == null"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAdd(scope.row)"
            v-hasPermi="['zndd:processdevice:edit']"
          >绑定</el-button>
          <!-- <el-button
            v-if="scope.row.processId != null"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zndd:processdevice:remove']"
          >解绑</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改工序绑定设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item> -->
        <el-form-item label="工序" prop="processId">
          <el-select v-model="form.processId" placeholder="请选择绑定工序" @change="processChange(form.processId)">
            <el-option v-for="dict in notBoundProcess" :key="dict.id" :label="dict.processName" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="工位" prop="stationId">
          <el-select v-model="form.stationId" placeholder="请选择绑定工位">
            <el-option v-for="dict in notBoundStation" :key="dict.id" :label="dict.stationName" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { notBoundProcessList, delProcessdevice, addProcessdevice, listAllDevice} from "@/api/zndd/process/processdevice";
import { listAllprocess } from "@/api/zndd/process/process";
import { listAllStation } from "@/api/zndd/process/station";

export default {
  name: "Processdevice",
  data() {
    return {
      //从父组件接收的工位id
      stationId:null,
      //存储选择的工序下未绑定工位信息
      notBoundStation:[],
      //存储未全部绑定工序信息
      notBoundProcess:[],
      //存储工序全部信息
      processAllList:[],
      //存储工位全部信息
      stationAllList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //查询列表基础数据
      processList:[],
      // 工序绑定设备信息表格数据
      processdeviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //查询参数
      query:{
        deviceName:null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        processId: null,
        stationId: null,
        deviceId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        processId: [
          { required: true, message: "工序不能为空", trigger: "blur" }
        ],
        stationId: [
          { required: true, message: "工位不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    //获取工序全部信息
    listAllprocess().then(response => {
      this.processAllList=response.rows;
    })
    //获取工位全部信息
    listAllStation().then(response => {
      this.stationAllList=response.rows;
    })
    this.getList();
  },
  methods: {
    //从工位界面接收数据
    pdMethods(satationId) {
      if (satationId != null && satationId != "undefind") {
        this.stationId = satationId;
      };
      this.getList();
    },
    //获取工序下面的工位
    processChange(processId){
      var processNo = null;
      this.processAllList.forEach(process => {
        if(processId == process.id){
          processNo = process.processNo;
        }
      });
      var list = [];
      this.stationAllList.forEach(station => {
        var flag = true;
        this.processdeviceList.forEach(processdevice => {
          if(station.id == processdevice.stationId){
            flag = false;
          }
        });
        if(station.processNo == processNo){
          if(flag){
            list.push(station);
          }
        }
      });
      this.notBoundStation = list;
    },
    //获取未全部绑定的工序信息
    notBoundList(){
      notBoundProcessList().then(response => {
        this.notBoundProcess = response.rows;
      });
    },
    //格式化设备绑定状态
    deviceStateeFomatter(row, column) {
      if(row.processId == null){
        return <el-tag  type="danger">未绑定</el-tag>;
      }else{
        return <el-tag>已绑定</el-tag>;
      }
    },
    //格式化工序信息
    processFomatter(row, column) {
      var ps = null;
      this.processAllList.forEach(process => {
        if (process.id == row.processId) {
          ps = process;
        }
      });
      if (ps == null) {
        return "";
      } else {
        return ps.processName;
      }
    },
    //格式化工位信息
    stationFomatter(row, column) {
      var sn = null;
      this.stationAllList.forEach(station => {
        if (station.id == row.stationId) {
          sn = station;
        }
      });
      if (sn == null) {
        return "";
      } else {
        return sn.stationName;
      }
    },
    /** 查询工序绑定设备信息列表 */
    getList() {
      this.loading = true;
      listAllDevice().then(response => {
        this.processdeviceList = response.rows;
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.notBoundList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        processId: null,
        stationId: null,
        deviceId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.notBoundStation = [];
      this.notBoundList();
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.query.deviceName != null){
        var list = [];
        this.processList.forEach(data => {
          if(data.device.indexOf(this.query.deviceName) != -1 ){
            list.push(data);
          }
        });
        this.processdeviceList = list;
      }else{
        this.processdeviceList = this.processList;
      }
      // this.queryParams.pageNum = 1;
      // this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.query.deviceName = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      // this.open = true;
      // this.title = "绑定工序设备信息";
      this.$modal.confirm('是否确认绑定该设备？').then(() =>{
        this.$nextTick(() => {
          console.log(this)
          this.form.deviceId = row.deviceId;
          this.form.stationId = this.stationId;
          this.form.processId = null;
          addProcessdevice(this.form).then(response => {
              this.$modal.msgSuccess("绑定成功");
              this.$emit("closeProcessdeviceFlag");
            });
        })
      })
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getProcessdevice(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改工序绑定设备信息";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.id != null) {
          //   updateProcessdevice(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          addProcessdevice(this.form).then(response => {
            this.$modal.msgSuccess("绑定成功");
            this.open = false;
            this.getList();
          });
          // }
        }
      });
      this.notBoundList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认解除绑定？').then(function() {
        return delProcessdevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("解除绑定成功");
      }).catch(() => {});
      this.notBoundList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/processdevice/export', {
        ...this.processdeviceList
      }, `设备绑定信息_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
